home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / graphics / gnuplot / gnubin.c < prev    next >
C/C++ Source or Header  |  1993-09-15  |  5KB  |  155 lines

  1. #ifndef lint
  2. static char *RCSid = "$Id: gnubin.c%v 3.50 1993/07/09 05:35:24 woo Exp $";
  3. #endif
  4.  
  5.  
  6. /*
  7.  * The addition of gnu_binary_files and binary_files, along with a small patch
  8.  * to command.c, will permit gnuplot to plot binary files.
  9.  * gnu_binary_files  - contains the code that relies on gnuplot include files
  10.  *                     and other definitions
  11.  * binary_files      - contains those things that are independent of those 
  12.  *                     definitions and files
  13.  *
  14.  * With these routines, hidden line removal of your binary data is possible!
  15.  *
  16.  * Last update: 3/3/92 for Gnuplot 3.24.
  17.  * Created from code for written by RKC for gnuplot 2.0b.
  18.  *
  19.  * 19 September 1992  Lawrence Crowl  (crowl@cs.orst.edu)
  20.  * Added user-specified bases for log scaling.
  21.  *
  22.  * Copyright (c) 1991,1992 Robert K. Cunningham, MIT Lincoln Laboratory
  23.  *
  24.  */
  25. #include <stdio.h>
  26. #include <math.h>
  27. #include "plot.h"
  28. #include "setshow.h"
  29.  
  30. /******************* SHARED INCLUDE FILE--start ***********************
  31.  *  I recommend that these be put into an include file that all
  32.  *  will share -- but I leave it up to the powers that be to do this.
  33.  */
  34. /* Copied from command.c -- this should be put in a shared macro file */
  35. #define inrange(z,min,max) ((min<max) ? ((z>=min)&&(z<=max)) : ((z>=max)&&(z<=min)) )
  36.  
  37. /* Routines for interfacing with command.c */
  38. float GPFAR *vector();
  39. float GPFAR *extend_vector();
  40. float GPFAR *retract_vector();
  41. float GPFAR * GPFAR *matrix();
  42. float GPFAR * GPFAR *extend_matrix();
  43. float GPFAR * GPFAR *retract_matrix();
  44. void free_matrix();
  45. void free_vector();
  46. /******************* SHARED INCLUDE FILE--end *************************/
  47. /*
  48.   Here we keep putting new plots onto the end of the linked list
  49.  
  50.   We assume the data's x,y values have x1<x2, x2<x3... and 
  51.                                        y1<y2, y2<y3... .
  52.   Actually, I think the assumption is less stron than that--it looks like
  53.   the direction just has to be the same.
  54.  
  55.   This routine expects the following to be properly initialized:
  56.       is_log_x, is_log_y, and is_log_z 
  57.       base_log_x, base_log_y, and base_log_z 
  58.       log_base_log_x, log_base_log_y, and log_base_log_z 
  59.       xmin,ymin, and zmin
  60.       xmax,ymax, and zmax
  61.       autoscale_lx, autoscale_ly, and autoscale_lz
  62.  
  63. */
  64. int
  65.   get_binary_data(this_plot,fp,p_ret_iso)
  66. struct surface_points *this_plot;
  67. FILE *fp;
  68. struct iso_curve **p_ret_iso;
  69. {
  70.   register int i,j;
  71.   float GPFAR * GPFAR *matrix, GPFAR *rt, GPFAR *ct;
  72.   int nr,nc;
  73.   int ndata;
  74.   struct iso_curve *this_iso;
  75.   float z;
  76.  
  77.   this_plot->plot_type = DATA3D;
  78.   this_plot->has_grid_topology = TRUE;
  79.  
  80.   if(!fread_matrix(fp,&matrix,&nr,&nc,&rt,&ct))
  81.     int_error("Binary file read error: format unknown!",NO_CARET);
  82.  
  83.   /* Now we do some error checking on the x and y axis */
  84.   if(is_log_x)
  85.     for(i=0; i<nc; i++)
  86.       if(ct[i] < 0.0)
  87.     int_error("X value must be above 0 for log scale!",NO_CARET);
  88.       else
  89.     ct[i] = log(ct[i])/log_base_log_x;
  90.  
  91.   if(is_log_y)
  92.     for(i=0; i<nr; i++)
  93.       if(rt[i] < 0.0)
  94.     int_error("Y value must be above 0 for log scale!",NO_CARET);
  95.       else
  96.     rt[i] = log(rt[i])/log_base_log_y;
  97.  
  98.   /* Count up the number of used column entries */
  99.   if (autoscale_lx) {
  100.     ndata = nc;
  101.     for(j=0; j< nc; j++){
  102.       if (ct[j] < xmin) xmin = ct[j];
  103.       if (ct[j] > xmax) xmax = ct[j];
  104.     }
  105.   }
  106.   else {
  107.     for(ndata = 0, j = 0; j< nc; j++){
  108.       if (!((ct[j] < xmin) || (ct[j] > xmax)))/*Column is in bounds*/
  109.     ndata++;
  110.     }
  111.   }
  112.  
  113.   for(i=0; i < nr; i++){
  114.       if (autoscale_ly) {
  115.     if (rt[i] < ymin) ymin = rt[i];
  116.     if (rt[i] > ymax) ymax = rt[i];
  117.       }
  118.       else if ((rt[i] < ymin) || (rt[i] > ymax))/* This row is out of bounds */
  119.     continue;
  120.  
  121.       this_iso = iso_alloc( ndata );/*Allocate the correct number of entries*/
  122.       for(ndata = 0, j = 0; j< nc; j++){/* Cycle through data */
  123.  
  124.     if ((ct[j] < xmin) || (ct[j] > xmax))/*Column is out of bounds*/
  125.       continue;       /* Only affects non-autoscale_lx cases */
  126.  
  127.     this_iso->points[ndata].x = ct[j];
  128.     this_iso->points[ndata].y = rt[i];
  129.     z = matrix[i][j];
  130.     if(is_log_z)
  131.       if (z <= 0.0)
  132.         int_error("Z value must be above 0 for log scale!",NO_CARET);
  133.       else
  134.         z = log(z)/log_base_log_z;
  135.     this_iso->points[ndata].z = z;
  136.       
  137.     if (autoscale_lz) {
  138.       if (z < zmin) zmin = z;
  139.       if (z > zmax) zmax = z;
  140.     }
  141.     ndata++;
  142.       }
  143.       this_iso->p_count = ndata;
  144.       this_iso->next = this_plot->iso_crvs;
  145.       this_plot->iso_crvs = this_iso;
  146.       this_plot->num_iso_read++;
  147.   }
  148.   
  149.   free_matrix(matrix,0,nr-1,0,nc-1);
  150.   free_vector(rt,0,nr-1);
  151.   free_vector(ct,0,nc-1);
  152.   *p_ret_iso = this_iso;
  153.   return(ndata+1);
  154. }
  155.